import {Component, OnInit} from '@angular/core';
import {AddSysMenuComponent} from '../add-sys-menu.component';
import {ErrRes} from '../../../../../frame/global/err-res';
import {SysMenu, SysMenuCondition} from '../../sys-menu.type';
import {Services} from '../../../../../frame/module/services';
import {HttpRequestClient} from '../../../../../frame/http-request-client/http-request-client';
import {UrlRou} from '../../../../../frame';

@Component({
    selector: 'app-main-add-sys-menu',
    templateUrl: './main-add-sys-menu.component.html',
    styleUrls: ['./main-add-sys-menu.component.css']
})
export class MainAddSysMenuComponent implements OnInit {

    errRes = new ErrRes();

    domain = new SysMenu();
    parentDomains = [];
    extend = {
        domain: {
            default: () => {
                this.domain.serAudit = 'WAIT_SUBMIT';
                this.domain.serUsing = 'ENABLE';
            },
            serAudit: () => {
                return 'serAudit';
            },
        },
    };

    func = {
        save: {
            req: (event) => {
                debugger;
                console.log("---> %o ", this.domain);
                Services.sysMenu.save(this.domain, this.func.save.callback).subscribe();
            },
            callback: {
                success: (result) => {
                    HttpRequestClient.router.navigate([UrlRou.sysMenu.list, {
                        moduleId: "system-management",
                        menuId: "sys-menu"
                    }]);
                },
                error: (result) => {
                    this.errRes.error = result.error;
                    this.errRes.statusText = result.statusText;
                }
            }
        }
    };

    initParentDomains = () => {
        const search = new SysMenuCondition();
        Services.sysMenu.list(search, {
            page: 0,
            size: 200,
            orders: 'serNum-asc'
        }).subscribe(item => {
            this.parentDomains = item.page.content.filter(item => item.parentId === null);
        });
    };

    constructor(public addSysMenu: AddSysMenuComponent) {
        this.addSysMenu.main = this;
    }

    ngOnInit() {
        this.extend.domain.default();
        this.initParentDomains();
    }

}
